package com.sf.iisp.util;

import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HttpRequest {

    private static final Logger LOGGER = LoggerFactory.getLogger(HttpRequest.class);

    public static String getHttpGetRequest(String url) {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpget = new HttpGet(url);

        try {
            CloseableHttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream instream = entity.getContent();
                try {
                    String msg = IOUtils.toString(instream);
                    return msg;
                } finally {
                    instream.close();
                }
            }

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String getHttpGetRequest(URI url) throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpget = new HttpGet(url);
        try {
            CloseableHttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream instream = entity.getContent();
                try {
                    String msg = IOUtils.toString(instream);
                    return msg;
                } finally {
                    instream.close();
                }
            }

        } catch (ClientProtocolException e) {
            LOGGER.error(e.getMessage(), e);
            throw e;
        } catch (IOException e) {
            LOGGER.error(e.getMessage(), e);
            throw e;
        }
        return null;
    }

    public static String getHttpPostRequest(String url) {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpget = new HttpPost(url);
        try {
            CloseableHttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream instream = entity.getContent();
                try {
                    String msg = IOUtils.toString(instream);
                    return msg;
                } finally {
                    instream.close();
                }
            }

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static String getHttpPostRequest(URI url) {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpget = new HttpPost(url);
        try {
            CloseableHttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream instream = entity.getContent();
                try {
                    String msg = IOUtils.toString(instream);
                    return msg;
                } finally {
                    instream.close();
                }
            }

        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    public static URI getUrl(String url, Map<String, String> parameterMap) throws URISyntaxException {
        URIBuilder ub = new URIBuilder().setScheme("http").setHost(url);
        for (Entry<String, String> entry : parameterMap.entrySet()) {
            ub.setParameter(entry.getKey(), entry.getValue());
        }
        return ub.build();
    }


    public static void main123(String[] args) throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        map.put("appId", "1");
        URI uri = getUrl("www.baidu.com", map);
        System.out.println(uri);

        String sourcePoolDetail = java.net.URLEncoder.encode("{\"list\":[{\"transId\":\"000000000000002\",\"transTimestamp\": \"2016-07-15 12:34:03\", \"msgType\" :\"1\", \"sysCode\":\"iisp\", \"virtualPhone\":\"9533855\",\"telephone0\":\"15889749737\", \"telephone1\":\"18675523334\"}]}", "UTF-8");

        Map<String, String> params = new HashMap<>();
        params.put("Operation", "0");
        params.put("CorpId", "886293");
        params.put("User", "admin");
        params.put("Pwd", "cinteam123");
        params.put("ObjId", "2016");
        params.put("DataType", "data");
        params.put("DataKey", "Telparser");
        params.put("DataSpace", "05200200");
        params.put("List", sourcePoolDetail);
        uri = getUrl("58.220.61.26:8078/dataInterface/action.do", params);
        System.out.println(uri);


        String response = HttpRequest.getHttpGetRequest(uri);
        System.out.println(response);

		/*CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpget = new HttpPost("http://183.60.52.234:8080/201511v3/getCdr?appId=1");
		try {
			CloseableHttpResponse response = httpclient.execute(httpget);
			 HttpEntity entity = response.getEntity();
			    if (entity != null) {
			        InputStream instream = entity.getContent();
			        try {
			        	String msg = IOUtils.toString(instream);
			        } finally {
			            instream.close();
			        }
			    }
			
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}*/
//        String  sourcePoolDetail = java.net.URLEncoder.encode("{\"list\":[{\"transId\":\"000000000000001\",\"transTimestamp\": \"2016-07-15 12:34:03\", \"msgType\" :\"1\", \"sysCode\":\"iisp\", \"telephone0\":\"15889749737\", \"telephone1\":\"18675523334\"}]}","UTF-8");
//        sourcePoolDetail = java.net.URLEncoder.encode(sourcePoolDetail,"UTF-8");
//        String  sourcePoolDetail1 = java.net.URLEncoder.encode("{\"list\":[{\"transId\":\"000000000000001\",\"transTimestamp\": \"2016-07-15 12:34:03\", \"msgType\" :\"1\", \"sysCode\":\"iisp\", \"status\":\"1\",\"telephone0\":\"15889749737\", \"telephone1\":\"18675523334\"}]}","UTF-8");
//
//        String str1 = "http://58.220.61.26:8078/dataInterface/action.do?Operation=1&WhereStr=transId&CorpId=886293&User=admin&Pwd=cinteam123&ObjId=2016&DataType=data&DataKey=Telparser&DataSpace=05200200&List=" +sourcePoolDetail1 ;
//
//        String str = "http://58.220.61.26:8078/dataInterface/action.do?Operation=0&CorpId=886293&User=admin&Pwd=cinteam123&ObjId=2016&DataType=data&DataKey=Telparser&DataSpace=05200200&List=" +sourcePoolDetail ;
//        //String str="http://58.220.61.26:8078/dataInterface/action.do?Operation=0&CorpId=**&User=**&Pwd=**&ObjId=7020&DataType=customer&DataKey=c1&DataSpace=20500300&List={\"list\":[{\"sourcePoolDetail\":\""+sourcePoolDetail+"\",\"city\":\"fuzhou\",\"country\":\"taijiang\",\"customerTel\":\"1234567890\"}]}";
//
//        System.out.println(str1);
//        try {
//            URL url=new URL(str1);
//            HttpURLConnection connection=(HttpURLConnection)url.openConnection();
//            connection.connect();
//            BufferedReader reader = new BufferedReader(new InputStreamReader(
//                    connection.getInputStream()));
//            System.out.println("=============================");
//            System.out.println("Contents of get request");
//            System.out.println("=============================");
//            String lines;
//            while ((lines = reader.readLine()) != null) {
//                System.out.println(lines);
//            }
//            reader.close();
//            // 断开连接
//            connection.disconnect();
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }

    
    public static void main(String[] args) throws Exception {
        Map<String, String> map = new HashMap<String, String>();
        map.put("appId", "1");
        URI uri = getUrl("10.202.37.140:8080/pub/qryInv", map);
        System.out.println(uri);

        String sourcePoolDetail = java.net.URLEncoder.encode("{\"list\":[{\"transId\":\"000000000000002\",\"transTimestamp\": \"2016-07-15 12:34:03\", \"msgType\" :\"1\", \"sysCode\":\"iisp\", \"virtualPhone\":\"9533855\",\"telephone0\":\"15889749737\", \"telephone1\":\"18675523334\"}]}", "UTF-8");

        Map<String, String> params = new HashMap<>();
        params.put("Operation", "0");
        params.put("CorpId", "886293");
        params.put("User", "admin");
        params.put("Pwd", "cinteam123");
        params.put("ObjId", "2016");
        params.put("DataType", "data");
        params.put("DataKey", "Telparser");
        params.put("DataSpace", "05200200");
        params.put("List", sourcePoolDetail);
        uri = getUrl("58.220.61.26:8078/dataInterface/action.do", params);
        System.out.println(uri);
        getHttpPostRequest(uri);
        
        
        
    }

}













